package org.chen.scheduler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.chen.config.BatchStatsManager;
import org.chen.model.CommentBatch;
import org.chen.enu.BatchStatus;
import org.chen.mapper.CommentBatchMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class BatchStatusSyncScheduler {

    @Autowired
    private BatchStatsManager batchStatsManager;

    @Autowired
    private CommentBatchMapper batchMapper;

    // 定时更新批次统计信息
    @Scheduled(fixedRate = 30000) // 每30秒执行一次
    public void scheduledBatchUpdate() {
        try {
            // 获取所有进行中的批次
            List<CommentBatch> activeBatches = batchMapper.selectList(
                    new QueryWrapper<CommentBatch>()
                            .in("status", BatchStatus.PROCESSING.name(), BatchStatus.PENDING.name())
            );

            for (CommentBatch batch : activeBatches) {
                batchStatsManager.updateBatchStats(batch.getBatchNo(), false);
            }
        } catch (Exception e) {
            log.error("定时更新批次统计信息失败", e);
        }
    }
}